/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.view;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import pkl51.controller.ActionController;
import pkl51.controller.CardKues;
import pkl51.controller.NavigationControl;
import pkl51.database.BackupDB;
import pkl51.model.Blok123Model;
import pkl51.util.Message;
import pkl51.view.kuesioner.PanelBlok123;

/**
 *
 * @author Hindarwan
 */
public class PanelExport extends javax.swing.JPanel {

    /**
     * Creates new form PanelUpdate
     */
    public PanelExport() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooserSaveFile = new javax.swing.JFileChooser();
        panelHead = new pkl51.component.PanelTransparan();
        labelStatus = new pkl51.component.Label();
        panelTransparan1 = new pkl51.component.PanelTransparan();
        buttonUpdateByNKS = new pkl51.component.ButtonGloss();
        jLabel1 = new javax.swing.JLabel();

        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(765, 554));

        panelHead.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        labelStatus.setForeground(new java.awt.Color(255, 255, 255));
        labelStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelStatus.setText("EXPORT");
        labelStatus.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N

        javax.swing.GroupLayout panelHeadLayout = new javax.swing.GroupLayout(panelHead);
        panelHead.setLayout(panelHeadLayout);
        panelHeadLayout.setHorizontalGroup(
            panelHeadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelHeadLayout.createSequentialGroup()
                .addComponent(labelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(619, Short.MAX_VALUE))
        );
        panelHeadLayout.setVerticalGroup(
            panelHeadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(labelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        panelTransparan1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        buttonUpdateByNKS.setText("Export");
        buttonUpdateByNKS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUpdateByNKSActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Tekan tombol export untuk melakukan backup data");

        javax.swing.GroupLayout panelTransparan1Layout = new javax.swing.GroupLayout(panelTransparan1);
        panelTransparan1.setLayout(panelTransparan1Layout);
        panelTransparan1Layout.setHorizontalGroup(
            panelTransparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTransparan1Layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addGroup(panelTransparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonUpdateByNKS, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelTransparan1Layout.setVerticalGroup(
            panelTransparan1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTransparan1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jLabel1)
                .addGap(62, 62, 62)
                .addComponent(buttonUpdateByNKS, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(337, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelHead, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelTransparan1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelHead, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelTransparan1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonUpdateByNKSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUpdateByNKSActionPerformed
        Thread t = new Thread(new BackupDB());
        t.start();
        File input = new File("backup.sql");
        FileWriter fw = null;
        FileReader fr = null;

        int showSaveDialog = jFileChooserSaveFile.showSaveDialog(this);
        File output = jFileChooserSaveFile.getSelectedFile();
        if (showSaveDialog == JFileChooser.APPROVE_OPTION) {

            {
                try {
                    fr = new FileReader(input);
                    fw = new FileWriter(output);
                    int c;

                    while ((c = fr.read()) != -1) {
                        fw.write(c);
                    }

                } catch (FileNotFoundException ex) {
                    Logger.getLogger(PanelExport.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(PanelExport.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    try {
                        fr.close();
                        fw.close();
                        Message.infoMessage("File berhasil diexport");
                    } catch (IOException ex) {
                        Logger.getLogger(PanelExport.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }//GEN-LAST:event_buttonUpdateByNKSActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private pkl51.component.ButtonGloss buttonUpdateByNKS;
    private javax.swing.JFileChooser jFileChooserSaveFile;
    private javax.swing.JLabel jLabel1;
    private pkl51.component.Label labelStatus;
    private pkl51.component.PanelTransparan panelHead;
    private pkl51.component.PanelTransparan panelTransparan1;
    // End of variables declaration//GEN-END:variables
}
